home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 018 / xlisp1.6 / msstuff.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  3KB  |  187 lines

  1. /* msstuff.c - ms-dos specific routines */
  2.  
  3. #include "xlisp.h"
  4.  
  5. #define LBSIZE 200
  6.  
  7. /* external routines */
  8. extern double ran();
  9.  
  10. /* external variables */
  11. extern NODE *s_unbound,*true;
  12. extern int prompt;
  13. extern int errno;
  14.  
  15. /* line buffer variables */
  16. static char lbuf[LBSIZE];
  17. static int  lpos[LBSIZE];
  18. static int lindex;
  19. static int lcount;
  20. static int lposition;
  21.  
  22. /* osinit - initialize */
  23. osinit(banner)
  24.   char *banner;
  25. {
  26.     printf("%s\n",banner);
  27.     lposition = 0;
  28.     lindex = 0;
  29.     lcount = 0;
  30. }
  31.  
  32. /* osrand - return a random number between 0 and n-1 */
  33. int osrand(n)
  34.   int n;
  35. {
  36.     n = (int)(ran() * (double)n);
  37.     return (n < 0 ? -n : n);
  38. }
  39.  
  40. /* osgetc - get a character from the terminal */
  41. int osgetc(fp)
  42.   FILE *fp;
  43. {
  44.     int ch;
  45.  
  46.     /* check for input from a file other than stdin */
  47.     if (fp != stdin)
  48.     return (agetc(fp));
  49.  
  50.     /* check for a buffered character */
  51.     if (lcount--)
  52.     return (lbuf[lindex++]);
  53.  
  54.     /* get an input line */
  55.     for (lcount = 0; ; )
  56.     switch (ch = xgetc()) {
  57.     case '\r':
  58.         lbuf[lcount++] = '\n';
  59.         xputc('\r'); xputc('\n'); lposition = 0;
  60.         lindex = 0; lcount--;
  61.         return (lbuf[lindex++]);
  62.     case '\010':
  63.     case '\177':
  64.         if (lcount) {
  65.             lcount--;
  66.             while (lposition > lpos[lcount]) {
  67.             xputc('\010'); xputc(' '); xputc('\010');
  68.             lposition--;
  69.             }
  70.         }
  71.         break;
  72.     case '\032':
  73.         osflush();
  74.         return (EOF);
  75.     default:
  76.         if (ch == '\t' || (ch >= 0x20 && ch < 0x7F)) {
  77.             lbuf[lcount] = ch;
  78.             lpos[lcount] = lposition;
  79.             if (ch == '\t')
  80.             do {
  81.                 xputc(' ');
  82.             } while (++lposition & 7);
  83.             else {
  84.             xputc(ch); lposition++;
  85.             }
  86.             lcount++;
  87.         }
  88.         else {
  89.             osflush();
  90.             switch (ch) {
  91.             case '\003':    xltoplevel();    /* control-c */
  92.             case '\007':    xlcleanup();    /* control-g */
  93.             case '\020':    xlcontinue();    /* control-p */
  94.             case '\032':    return (EOF);    /* control-z */
  95.             default:        return (ch);
  96.             }
  97.         }
  98.     }
  99. }
  100.  
  101. /* osputc - put a character to the terminal */
  102. osputc(ch,fp)
  103.   int ch; FILE *fp;
  104. {
  105.     /* check for output to something other than stdout */
  106.     if (fp != stdout)
  107.     return (aputc(ch,fp));
  108.  
  109.     /* check for control characters */
  110.     oscheck();
  111.  
  112.     /* output the character */
  113.     if (ch == '\n') {
  114.     xputc('\r'); xputc('\n');
  115.     lposition = 0;
  116.     }
  117.     else {
  118.     xputc(ch);
  119.     lposition++;
  120.    }
  121. }
  122.  
  123. /* oscheck - check for control characters during execution */
  124. oscheck()
  125. {
  126.     int ch;
  127.     if (ch = xcheck())
  128.     switch (ch) {
  129.     case '\002':    osflush(); xlbreak("BREAK",s_unbound); break;
  130.     case '\003':    osflush(); xltoplevel(); break;
  131.     }
  132. }
  133.  
  134. /* osflush - flush the input line buffer */
  135. osflush()
  136. {
  137.     lindex = lcount = 0;
  138.     osputc('\n',stdout);
  139.     prompt = 1;
  140. }
  141.  
  142. /* xgetc - get a character from the terminal without echo */
  143. static int xgetc()
  144. {
  145.     return (bdos(7));
  146. }
  147.  
  148. /* xputc - put a character to the terminal */
  149. static xputc(ch)
  150.   int ch;
  151. {
  152.     bdos(6,ch);
  153. }
  154.  
  155. /* xcheck - check for a character */
  156. static int xcheck()
  157. {
  158.     return (bdos(6,0xFF));
  159. }
  160.  
  161. /* xdos - execute a dos command */
  162. NODE *xdos(args)
  163.   NODE *args;
  164. {
  165.     char *cmd;
  166.     cmd = xlmatch(STR,&args)->n_str;
  167.     xllastarg(args);
  168.     return (system(cmd) == -1 ? cvfixnum((FIXNUM)errno) : true);
  169. }
  170.  
  171. /* xgetkey - get a key from the keyboard */
  172. NODE *xgetkey(args)
  173.   NODE *args;
  174. {
  175.     xllastarg(args);
  176.     return (cvfixnum((FIXNUM)xgetc()));
  177. }
  178.  
  179. /* osfinit - initialize pc specific functions */
  180. osfinit()
  181. {
  182.     xlsubr("DOS",        SUBR,    xdos);
  183.     xlsubr("GET-KEY",        SUBR,    xgetkey);
  184. }
  185.  
  186.  
  187.